model.request = {
    getUserWishes : function(userID, callback) {

    	if (model.storage.wishes.loaded(userID) && $.isFunction(callback)) {
    		callback();
    		return;
    	}
    	
    	var params = {"user_id" : userID};
        
        model.request.call("wish/list", params, function(data) {
        	
        	model.storage.wishes.setList(userID, data);
        	        	    
        	if ($.isFunction(callback)) {        		
        		callback();
        	}
        });
    },
    
    getSelectedWishes : function(method, params, callback) {
    	
    	var type = method;
    	if (!utils.empty(params.filter)) type = method + "_" + params.filter;
    	
    	if (model.storage.wishes.loaded(type) && $.isFunction(callback)) {
    		callback();
    		return;
    	}
    	
    	model.request.call("wish/" + method, params, function(data) {
    		
    		model.storage.wishes.setList(type, data);
    		
    		if ($.isFunction(callback)) {        		
    			callback();
    		}
    	});
    },
    
    removeWish : function(params, callback) {

    	model.request.call("wish/remove", params, function(data) {
    		
    		var userID = viewer.id;
    		model.storage.wishes.remove(userID, data);
    		
        	if ($.isFunction(callback)) {        		
        		callback(data);
        	}
        	
        }, gadgets.io.MethodType.POST);
    },
    
    likeWish : function(wish_id, callback) {
    	
    	params = {"wish_id" : wish_id};
    	
    	model.request.call("wish/like", params, function(data) {

    		if ($.isFunction(callback)) {        		
    			callback(data);
    		}
    		
    	}, gadgets.io.MethodType.POST);
    },
    
    addWish : function(params, callback) {

    	params["username"] = viewer["name"];
    	
    	model.request.call("wish/add", params, function(data) {
        	
    		model.storage.wishes.add(viewer.id, data);
    		
        	if ($.isFunction(callback)) {        		
        		callback(data);
        	}

        }, gadgets.io.MethodType.POST);
    },
    
    editWish : function(params, callback) {
    	
    	model.request.call("wish/change", params, function(data) {
    		
    		var userID = viewer.id;
    		
    		model.storage.wishes.replace(userID, data);
    		
        	if ($.isFunction(callback)) {        		
        		callback();
        	}
    		
    	}, gadgets.io.MethodType.POST);
    },

    addReserve : function(wish_id, callback) {
    	
    	var index = "_reserves_";
    	
    	params = {"wish_id" : wish_id};
    	model.request.call("reservation/add", params, function(data) {

    		model.storage.wishes.add(index, data);
    		model.storage.wishes.replace(data.owner_id, data);
    		if ($.isFunction(callback)) {
    			var data = model.storage.wishes.getList(index);
    			callback(data);
    		}
    		    		
    	}, gadgets.io.MethodType.POST);
    },
    
    removeReserve : function(wish_id, callback) {
    	
    	var index = "_reserves_";
    	
    	params = {"wish_id" : wish_id};
    	model.request.call("reservation/remove", params, function(data) {
    		
    		model.storage.wishes.remove(index, wish_id);
    		model.storage.wishes.replace(data.owner_id, data);
    		
    		if ($.isFunction(callback)) {   
        		var data = model.storage.wishes.getList(index);
        		callback(data);
        	}
    		
    	}, gadgets.io.MethodType.POST);
    },
    
    getReserves : function(callback) {
    	
    	var index = "_reserves_";
    	
    	if (model.storage.wishes.loaded(index) && $.isFunction(callback)) {
    		var data = model.storage.wishes.getList(index);
    		callback(data);
    		return;
    	}
    	
    	model.request.call("reservation/list", null, function(data) {
    		
    		model.storage.wishes.setList(index, data);

    		if ($.isFunction(callback)) {      		
        		var data = model.storage.wishes.getList(index);
        		callback(data);
        	}
    	});
    },
    
    sendNotification : function(userId, title, body) {
	  var params = [];
	  params[opensocial.Message.Field.TITLE] = title;
	  params[opensocial.Message.Field.TYPE] = opensocial.Message.Type.NOTIFICATION;

	  var message = opensocial.newMessage(body, params);
	  var recipient = userId;
	  opensocial.requestSendMessage(recipient, message, function(data) {
		  
	       	if (data.hadError()) controller.common.showError();
	  });
	},
	
	inviteFriends : function() {
		vz.invite.getUniqueToken(function(token) {
		    vz.invite.create(token, utils.getText("invite.title"));
        });
    },
    
    postToFeed : function(data) {

    	var params = [];
    	params[opensocial.Activity.Field.TITLE] = data.title;
    	params[opensocial.Activity.Field.BODY] = data.message;
    	 
    	var activity = opensocial.newActivity(params);
    	 
    	opensocial.requestCreateActivity(activity, null, function(submitted) {
    	    //console.log('submitted :', submitted);
    	});
    },
	
	postToWall : function(userId, message) {
	     var params = [];
	     params[opensocial.Message.Field.TYPE] = opensocial.Message.Type.PUBLIC_MESSAGE;
	     var message = opensocial.newMessage(message, params);
	     var recipient = userId;
	     opensocial.requestSendMessage(recipient, message, callback);
	},
    
    call : function(uri, data, callback, method) {
    	
		$("#system-message").hide();
    	
    	method = method || gadgets.io.MethodType.GET;
    	
    	var params = {};
    	params[gadgets.io.RequestParameters.CONTENT_TYPE]  = gadgets.io.ContentType.JSON;
    	params[gadgets.io.RequestParameters.METHOD]        = method;
    	params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.SIGNED;
    	
    	var url = config.api_host + uri;
    	
    	if (typeof data === 'object') {
    		
    		var parameters = gadgets.io.encodeValues(data);
    		
    		if (method === gadgets.io.MethodType.GET) {
    			
    			url += "?" + parameters;
    			
    		} else {        		
    			params[gadgets.io.RequestParameters.POST_DATA] = parameters;
    		}
    	}
    	
    	view.common.lockScreen();
    	
    	gadgets.io.makeRequest(url, function(obj) {
    		
    		view.common.unlockScreen();
    		
    		if (obj.rc == 200 && obj.data.message.meta && obj.data.message.meta.status == 200) {
    			
    			user_uid = obj.data.message.meta.uid;
    			
    			if (typeof callback === 'function') {
    				callback(obj.data.message.data);
    			}
    			
    		} else {
    			
    			if (method == gadgets.io.MethodType.POST) {
    				view.common.showErrorDialog(gt("error.general"));
    				return;
    			}
    			
    			if (utils.empty(data)) data = {};
    			
        		if (utils.empty(data._counter)) data._counter = 0;
        		
    			data._counter++;
    			
    			if (data._counter < 3) {
    				model.request.call(uri, data, callback, method);
    			} else {
    				view.common.showErrorDialog(utils.getText("error.general"));
    			}
    		}
    	}, params);
    }
};
